//
//  DHS2_BuyMemberController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/18.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_BuyMemberController.h"
#import "DHMemberCardView.h"
#import "NewPagedFlowView.h"
#import "DHPayViewController.h"

@interface DHS2_BuyMemberController ()<NewPagedFlowViewDelegate, NewPagedFlowViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *dingyueLabel;
@property (weak, nonatomic) IBOutlet UILabel *zhekouLabel;
@property (weak, nonatomic) IBOutlet UILabel *guwenLabel;
@property (weak, nonatomic) IBOutlet UILabel *liangtiLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *yajinLabel;
@property (weak, nonatomic) IBOutlet UILabel *youhuiLabel;
@property (weak, nonatomic) IBOutlet UIButton *buyButton;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (strong, nonatomic) NSDictionary *vipCategory;
@property (strong, nonatomic) NSArray *vipListArray;

@property (nonatomic, strong) NewPagedFlowView *pageFlowView;

@property (strong, nonatomic) NSDictionary *couponDic; //购买会员优惠券

@end

@implementation DHS2_BuyMemberController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"购买会员";
    
    self.buyButton.layer.cornerRadius = self.buyButton.height / 2;
    
    // 获取购买会员优惠券
    [self loadVIPPromotion];
    
    [self.buyButton bk_addEventHandler:^(id sender) {
        NSString *coupon_id;
        if (self.couponDic  && ![self.couponDic isEqual:[NSNull null]]) {
            coupon_id = [self.couponDic objectForKey:@"_id"];
        }
        if ([self.vipCategory objectForKey:@"uuid"]) {
            CGFloat couponPrice = 0;
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            if (coupon_id) {
                [params setObject:coupon_id forKey:@"coupon_id"];
                couponPrice = [[self.couponDic objectForKey:@"discount"] floatValue];
            }
            [params setObject:[self.vipCategory objectForKey:@"uuid"] forKey:@"uuid"];
            
            [[DHNetAPIClient shareDHNetAPIClient] request_vip_purchaseWithParams:params andBlock:^(id data, NSError *error) {
                if (data) {
                    NSDictionary *subscription = [data objectForKey:@"subscription"];
                    double price = [[subscription objectForKey:@"price"] doubleValue];
                    double deposit = [[subscription objectForKey:@"deposit"] doubleValue];
                    NSNumber *totalPrice = [NSNumber numberWithDouble:price + deposit - couponPrice - [GlobalManager shareGlobalManager].deposit];
                    DHPayViewController *payController = [[DHPayViewController alloc] initWithPrice:totalPrice andVipID:[subscription objectForKey:@"vip_uuid"] andVipName:[self.vipCategory objectForKey:@"name"] preOrderId:[subscription objectForKey:@"_id"]];
                    payController.payType = PayTypeVip;
                    [self.navigationController pushViewController:payController animated:YES];
                }
            }];
        }else{
            [NSObject showHudTipStr:@"请选择VIP类型"];
        }
    } forControlEvents:UIControlEventTouchUpInside];
}

- (NSArray *)vipListArray{
    if (!_vipListArray) {
        _vipListArray = [NSArray array];
    }
    return _vipListArray;
}

- (void)loadVIPPromotion{
    [[DHNetAPIClient shareDHNetAPIClient] request_user_vip_promotionAndBlock:^(id data, NSError *error) {
        if (data) {
            self.couponDic = [data objectForKey:@"coupon"];
            
            if (self.couponDic && ![self.couponDic isEqual:[NSNull null]]) {
                NSString *coupon_id = [self.couponDic objectForKey:@"_id"];
                CGFloat couponPrice = 0;
                if (coupon_id) {
                    couponPrice = [[self.couponDic objectForKey:@"discount"] floatValue];
                    self.youhuiLabel.text = [NSString stringWithFormat:@"新购会员优惠券 -%.1f 元",couponPrice];
                }
            }
            
            [self loadVIPCategory];
        }
    }];
}

- (void)loadVIPCategory{
    [[DHNetAPIClient shareDHNetAPIClient] request_vipLevelsAndBlock:^(id data, NSError *error) {
        self.vipListArray = [data objectForKey:@"vips"];
        self.vipCategory = self.vipListArray[0];
        [self updateUI];
        
        NSDictionary *obj = [self.vipListArray objectAtIndex:0];
        CGFloat zhekou = [[obj objectForKey:@"discount"] floatValue];
        
        NSString *zhekouStr = [NSString stringWithFormat:@"%@",[obj objectForKey:@"discount"]];
        if (zhekouStr.length > 3) {
            self.zhekouLabel.text = [NSString stringWithFormat:@"%.1f折",zhekou * 10];
        }else if ([zhekouStr integerValue] == 1){
            self.zhekouLabel.text = @"无折扣";
        }
        else{
            self.zhekouLabel.text = [NSString stringWithFormat:@"%.0f折",zhekou * 10];
        }
        
        
        self.guwenLabel.text = [NSString stringWithFormat:@"每月可资讯穿搭顾问%ld次",[[obj objectForKey:@"modelling"] integerValue]];
        self.liangtiLabel.text = [NSString stringWithFormat:@"每月可上门量体%ld次",[[obj objectForKey:@"measure"] integerValue]];
        
        CGFloat price = [[obj objectForKey:@"price"] floatValue];
        CGFloat discount = [[obj objectForKey:@"discount"] floatValue];
        if ([[obj objectForKey:@"name"] isEqualToString:@"租衣包月"]) {
            self.priceLabel.text = [NSString stringWithFormat:@"%.2f元/月",price * discount];
        }else if ([[obj objectForKey:@"name"] isEqualToString:@"租衣季卡"]){
            self.priceLabel.text = [NSString stringWithFormat:@"%.2f元/季度",price * discount];
        }else if ([[obj objectForKey:@"name"] isEqualToString:@"租衣半年卡"]){
            self.priceLabel.text = [NSString stringWithFormat:@"%.2f元/半年",price * discount];
        }else if ([[obj objectForKey:@"name"] isEqualToString:@"租衣年卡"]){
            self.priceLabel.text = [NSString stringWithFormat:@"%.2f元/年",price * discount];
        }
        
        self.yajinLabel.text = [NSString stringWithFormat:@"%.1f元",[[obj objectForKey:@"deposit"] floatValue]];
        
        CGFloat couponPrice = 0;
        if (self.couponDic && ![self.couponDic isEqual:[NSNull null]]) {
            NSString *coupon_id = [self.couponDic objectForKey:@"_id"];
            if (coupon_id) {
                couponPrice = [[self.couponDic objectForKey:@"discount"] floatValue];
                self.youhuiLabel.text = [NSString stringWithFormat:@"新购会员优惠券 -%.1f 元",couponPrice];
            }
        }
        [self.buyButton setTitle:[NSString stringWithFormat:@"立即订阅：%.1f元",price * discount - couponPrice] forState:UIControlStateNormal];
    }];
}

- (void)updateUI{
    CGFloat space = (self.scrollView.width - 260) / 4;
    CGFloat spaceY = (self.scrollView.height - 143) / 2;
    
    NewPagedFlowView *pageFlowView = [[NewPagedFlowView alloc] initWithFrame:CGRectMake(0, self.scrollView.top + 1, self.scrollView.width, self.scrollView.height)];
    pageFlowView.backgroundColor = [UIColor whiteColor];
    pageFlowView.delegate = self;
    pageFlowView.dataSource = self;
    pageFlowView.minimumPageAlpha = 0.4;
    
#warning 假设产品需求左右卡片间距30,底部对齐
    pageFlowView.leftRightMargin = space;
    pageFlowView.topBottomMargin = spaceY;
    
    pageFlowView.orginPageCount = self.vipListArray.count;
    pageFlowView.isOpenAutoScroll = NO;
    
    //初始化pageControl
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, pageFlowView.frame.size.height - 15, kScreenWidth, 8)];
    pageFlowView.pageControl = pageControl;
//    [pageFlowView addSubview:pageControl];
    [pageFlowView reloadData];
    [self.view addSubview:pageFlowView];
    self.pageFlowView = pageFlowView;
}

#pragma mark --NewPagedFlowView Delegate
- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex {
    
    NSLog(@"点击了第%ld张图",(long)subIndex + 1);
}

- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(NewPagedFlowView *)flowView {
    NSDictionary *obj = [self.vipListArray objectAtIndex:pageNumber];
    self.vipCategory = obj;
    CGFloat zhekou = [[obj objectForKey:@"discount"] floatValue];
    
    NSString *zhekouStr = [NSString stringWithFormat:@"%@",[obj objectForKey:@"discount"]];
    if (zhekouStr.length > 3) {
        self.zhekouLabel.text = [NSString stringWithFormat:@"%.1f折",zhekou * 10];
    }else if ([zhekouStr integerValue] == 1){
        self.zhekouLabel.text = @"无折扣";
    }
    else{
        self.zhekouLabel.text = [NSString stringWithFormat:@"%.0f折",zhekou * 10];
    }
    
    self.guwenLabel.text = [NSString stringWithFormat:@"每月可资讯穿搭顾问%ld次",[[obj objectForKey:@"modelling"] integerValue]];
    self.liangtiLabel.text = [NSString stringWithFormat:@"每月可上门量体%ld次",[[obj objectForKey:@"measure"] integerValue]];
    
    CGFloat price = [[obj objectForKey:@"price"] floatValue];
    CGFloat discount = [[obj objectForKey:@"discount"] floatValue];
    if ([[obj objectForKey:@"name"] isEqualToString:@"租衣包月"]) {
        self.priceLabel.text = [NSString stringWithFormat:@"%.2f元/月",price * discount];
    }else if ([[obj objectForKey:@"name"] isEqualToString:@"租衣季卡"]){
        self.priceLabel.text = [NSString stringWithFormat:@"%.2f元/季度",price * discount];
    }else if ([[obj objectForKey:@"name"] isEqualToString:@"租衣半年卡"]){
        self.priceLabel.text = [NSString stringWithFormat:@"%.2f元/半年",price * discount];
    }else if ([[obj objectForKey:@"name"] isEqualToString:@"租衣年卡"]){
        self.priceLabel.text = [NSString stringWithFormat:@"%.2f元/年",price * discount];
    }
    
    self.yajinLabel.text = [NSString stringWithFormat:@"%.1f元",[[obj objectForKey:@"deposit"] floatValue]];
    
    CGFloat couponPrice = 0;
    if (self.couponDic && ![self.couponDic isEqual:[NSNull null]]) {
        NSString *coupon_id = [self.couponDic objectForKey:@"_id"];
        if (coupon_id) {
            couponPrice = [[self.couponDic objectForKey:@"discount"] floatValue];
            self.youhuiLabel.text = [NSString stringWithFormat:@"新购会员优惠券 -%.1f 元",couponPrice];
        }
    }
    [self.buyButton setTitle:[NSString stringWithFormat:@"立即订阅：%.1f元",price * discount - couponPrice] forState:UIControlStateNormal];
}

#warning 假设产品需求左右中间页显示大小为 Width - 50, (Width - 50) * 9 / 16
- (CGSize)sizeForPageInFlowView:(NewPagedFlowView *)flowView {
    return CGSizeMake(260, 143);
}

#pragma mark --NewPagedFlowView Datasource
- (NSInteger)numberOfPagesInFlowView:(NewPagedFlowView *)flowView {
    
    return self.vipListArray.count;
}

- (PGIndexBannerSubiew *)flowView:(NewPagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index{

    DHMemberCardView *bannerView = (DHMemberCardView *)[flowView dequeueReusableCell];
    if (!bannerView) {
        bannerView = [DHMemberCardView shareDHMemberCardView];
    }
    
    NSDictionary *obj = [self.vipListArray objectAtIndex:index];
    NSDate *nowDate = [NSDate date];
    if ([[obj objectForKey:@"name"] isEqualToString:@"租衣包月"]) {
        bannerView.imgView.image = [UIImage imageNamed:@"month"];
        NSString *startTime = [NSDate stringFromDate:nowDate withFormat:@"yyyy.MM.dd"];
        NSString *endTime = [NSDate stringFromDate:[nowDate dateByAddingMonths:1] withFormat:@"yyyy.MM.dd"];
        bannerView.timeLabel.text = [NSString stringWithFormat:@"%@ - %@",startTime, endTime];
        CGFloat price = [[obj objectForKey:@"price"] floatValue];
        bannerView.priceLabel.text = [NSString stringWithFormat:@"%.0f元/月",price];
    }else if ([[obj objectForKey:@"name"] isEqualToString:@"租衣季卡"]){
        bannerView.imgView.image = [UIImage imageNamed:@"season"];
        NSString *startTime = [NSDate stringFromDate:nowDate withFormat:@"yyyy.MM.dd"];
        NSString *endTime = [NSDate stringFromDate:[nowDate dateByAddingMonths:4] withFormat:@"yyyy.MM.dd"];
        bannerView.timeLabel.text = [NSString stringWithFormat:@"%@ - %@",startTime, endTime];
        CGFloat price = [[obj objectForKey:@"price"] floatValue];
        bannerView.priceLabel.text = [NSString stringWithFormat:@"%.0f元/季度",price];
    }else if ([[obj objectForKey:@"name"] isEqualToString:@"租衣半年卡"]){
        bannerView.imgView.image = [UIImage imageNamed:@"half_year"];
        NSString *startTime = [NSDate stringFromDate:nowDate withFormat:@"yyyy.MM.dd"];
        NSString *endTime = [NSDate stringFromDate:[nowDate dateByAddingMonths:6] withFormat:@"yyyy.MM.dd"];
        bannerView.timeLabel.text = [NSString stringWithFormat:@"%@ - %@",startTime, endTime];
        CGFloat price = [[obj objectForKey:@"price"] floatValue];
        bannerView.priceLabel.text = [NSString stringWithFormat:@"%.0f元/半年",price];
    }else if ([[obj objectForKey:@"name"] isEqualToString:@"租衣年卡"]){
        bannerView.imgView.image = [UIImage imageNamed:@"year"];
        NSString *startTime = [NSDate stringFromDate:nowDate withFormat:@"yyyy.MM.dd"];
        NSString *endTime = [NSDate stringFromDate:[nowDate dateByAddingMonths:12] withFormat:@"yyyy.MM.dd"];
        bannerView.timeLabel.text = [NSString stringWithFormat:@"%@ - %@",startTime, endTime];
        CGFloat price = [[obj objectForKey:@"price"] floatValue];
        bannerView.priceLabel.text = [NSString stringWithFormat:@"%.0f元/年",price];
    }
    bannerView.nameLabel.text = [obj objectForKey:@"name"];

    return bannerView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
